import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_node_list.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';
import 'package:app/src/ui/route/works/public/review_submitted.dart';

import '../../../../http/bean/works_2023/purcontract_bean.dart';
import '../../../../http/bean/worksdo/material_requisition_bean.dart';
import '../../../../http/bean/wroks/search_bean.dart';
import '../../../../http/bean/ywx_limit.dart';
import '../../../../http/model/review_model.dart';
import '../../../dialog/dailog.dart';
import '../../helper/route_helper.dart';
import '../../worksdo/type16_material_requisition/public/items/ui_attachment.dart';
import 'acom/ui_purcontract_audit_form.dart';
import 'acom/ui_purcontract_audit_form2.dart';

///采购合同审核详情,跳转到 查看历史合同
class PurcontractAuditDetailsHistoryRoute extends StatefulWidget {
  YwxItem item;
  PurContractBean? data;

  PurcontractAuditDetailsHistoryRoute(this.item, this.data);

  @override
  _State createState() => _State();
}

class _State extends State<PurcontractAuditDetailsHistoryRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    if (presenter.searchInfo == null) {
      presenter.searchInfo = SearchInfo(widget.item)
        ..isShowStatus = false //审核状态
        ..isShowStatusAll = false //审核状态不具备全显示
        ..isPurOrderCode = true //采购单号
        ..isShowMaterialName = true //物料名称
        ..isPurBodyCode = true //子合同编号
        ..isShowOutCode = true //子合同外部编号
        ..isShowSignSDateTime = true //签订日期
        ..isShowAddTime = false; //申请日期
    }
    presenter.getContractapplygetinfoWithDelay2(
        context, widget.data?.PurContractCode); //获取审核详情数据
  }

  @override
  Widget build(BuildContext context) {
    px.init(context);
    presenter.searchInfo?.ShowOutCodeName =
        AppStrings.of(context).ContractOutCode; //子合同外部编号
    var scaffold = AppUi.getScaffoldWithReView(
        context, _body(context), widget.item, presenter.searchInfo!, () {
      //print("===============================================================搜索刷新");
      presenter.setStateBody?.call(() {
        presenter?.getContractapplygetinfo2(
            context, widget.data?.PurContractCode,
            callback: null); //获取审核详情数据
      });
    }, title: AppStrings.of(context).tohistoryhetong2); //采购合同审核-历史合同信息
    return WillPopScope(
      child: scaffold,
      onWillPop: () async {
        return true; //true 不屏蔽返回键
      },
    );
  }

  Widget _body(BuildContext context) {
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
      presenter.setStateBody = setState;
      // if (StringUtils.isEmpty(presenter?.mPDatas)) {
      //   return AppUi.getEmptyView();
      // }
      List<Widget> children = [];
      //搜索条件
      var searchLayout =
          AppUi.getSearchLayout(context, presenter.searchInfo!, () {
        presenter.setStateBody?.call(() {
          presenter?.getContractapplygetinfo2(
              context, widget.data?.PurContractCode,
              callback: null); //获取审核详情数据
        });
      });
      children.add(searchLayout);
      presenter?.mPDatas?.forEach((element) {
        children.add(UiPurContractAuditForm2.build(
            context, element, widget.item)); //物料信息
      });
      return Column(
        children: [
          Expanded(child: UiBase.listView(context, children)),
          Visibility(
              child: ReviewSubmittedStateful(
                presenter?.mDatas,
                hasBack: true,
                onlyBack: true,
                hasEnd: false,
                hasAuditOpinion: false,
              ),
              visible: true)
        ],
      );
      return Column(
        children: children,
      );
    });
  }
}

class _Presenter extends ReviewPresenter {
  ///搜索条件
  SearchInfo? searchInfo = null;

  ///body视图状态
  StateSetter? setStateBody;

  List<PurContractBean>? mPDatas = null;

  ///获取审核详情数据
  getContractapplygetinfo2(BuildContext context, String? PurContractCode,
      {ResponseCallbackWithVoid? callback = null}) {
    isLoad = true;
    //获取详情数据
    ReviewModel.getContractapplygetinfoHistory(context, true,
        PurContractCode: PurContractCode,
        searchInfo: searchInfo, onError: (code, error) {
      isLoad = false;
      setStateBody?.call(() {});
      AppDialog.showDialogWithError(context, code, error)?.then((value) {
        AppRouteHelper.pop(context); //网络异常，关闭当前页面
      });
    })?.then((value) {
      isLoad = false;
      //print("是否成功：${value.isSuccessForCode()}\t数据个数：${value?.Data?.length}");
      if (value.isSuccessForCode()) {
        try {
          setStateBody?.call(() {
            mPDatas = value?.Data;
          });
          callback?.call();
        } catch (e) {}
      } else {
        //异常
        AppDialog.showDialogWithInfo(context, content: value.MsgDetail)
            ?.then((value) {
          AppRouteHelper.pop(context); //关闭当前页面
        });
      }
    });
  }

  ///获取审核详情数据[延迟获取，等待页面加载完成]
  getContractapplygetinfoWithDelay2(
      BuildContext context, String? PurContractCode,
      {ResponseCallbackWithVoid? callback = null}) {
    Future.delayed(Duration(milliseconds: 200)).then((e) {
      getContractapplygetinfo2(context, PurContractCode,
          callback: callback); //获取审核详情数据
    });
  }
}
